package funs

type FieldColumn struct {
	JsonName   string `json:"json_name" config:"json_name" toml:"json_name"`
	ColumnName string `json:"column_name" config:"column_name" toml:"column_name"`
}

type ImporatPath struct {
	Name string `json:"name" config:"name" toml:"name"`
	Path string `json:"path" config:"path" toml:"path"`
}

type Filed struct {
	ImporatPaths    []ImporatPath       `config:"import_paths" json:"import_paths" toml:"import_paths"`
	JsonNameRule    bool                `config:"json_name_rule" json:"json_name_rule" toml:"json_name_rule"`
	TablePrefix     string              `config:"table_prefix" json:"table_prefix" toml:"table_prefix"`
	FieldColumns    []FieldColumn       `config:"field_columns" json:"field_columns" toml:"field_columns"`
	ColumnTypes     map[string]string   `config:"column_types" json:"column_types" toml:"column_types"`
	ColumnNameTypes map[string][]string `config:"column_name_types" json:"column_name_types" toml:"column_name_types"`
	CreatedAt       string              `config:"created_at" json:"created_at" toml:"created_at"`
	UpdatedAt       string              `config:"updated_at" json:"updated_at" toml:"updated_at"`
	DeletedAt       string              `config:"deleted_at" json:"deleted_at" toml:"deleted_at"`
}

type Model struct {
	TableName string `json:"table_name" config:"table_name" toml:"table_name"`
	Filed
}

type Config struct {
	DbType        string   `config:"db_type" json:"db_type" toml:"db_type"`
	DbConn        string   `config:"db_conn" json:"db_conn" toml:"db_conn"`
	IsClean       bool     `config:"is_clean" json:"is_clean" toml:"is_clean"`
	RootPath      string   `config:"root_path" json:"root_path" toml:"root_path"`
	CfgPath       string   `config:"cfg_path" json:"cfg_path" toml:"cfg_path"`
	OutPath       string   `config:"out_path" json:"out_path" toml:"out_path"`
	ExcludeTables []string `config:"exclude_tables" json:"exclude_tables" toml:"exclude_tables"`
	Filed
}

type FixedField struct {
	CreatedAt string
	UpdatedAt string
	DeletedAt string
}

func (f *FixedField) Set(c Filed) {
	f.CreatedAt = c.CreatedAt
	f.UpdatedAt = c.UpdatedAt
	f.DeletedAt = c.DeletedAt

}

type FixedFieldOld struct {
	FixedField
	Old *FixedField
}

func (f *FixedFieldOld) Set(c Filed) {
	f.FixedField.Set(c)
	if f.Old == nil {
		f.Old = &FixedField{}
		f.Old.Set(c)
		if len(f.CreatedAt) == 0 {
			f.Old.CreatedAt = "created_at"
		}
		if len(f.UpdatedAt) == 0 {
			f.Old.UpdatedAt = "updated_at"
		}
		if len(f.DeletedAt) == 0 {
			f.Old.DeletedAt = "deleted_at"
		}
	}
}

func (f *FixedFieldOld) Get() *FixedField {
	ff := &FixedField{}
	if len(f.CreatedAt) == 0 {
		ff.CreatedAt = f.Old.CreatedAt
	} else {
		ff.CreatedAt = f.CreatedAt
	}
	if len(f.UpdatedAt) == 0 {
		ff.UpdatedAt = f.Old.UpdatedAt
	} else {
		ff.UpdatedAt = f.UpdatedAt
	}
	if len(f.DeletedAt) == 0 {
		ff.DeletedAt = f.Old.DeletedAt
	} else {
		ff.DeletedAt = f.DeletedAt
	}
	return ff
}
